
var typeDatas = com.selectDictDatas("diy_category_type")
var yesNoDatas = com.selectDictDatas("sys_yes_no")

// console.log(typeDatas);
$(function () {
    // $('input[id=excelFile]').on('change', function (e) {
    //     debugger;
    //     $('button[type=button]').on('click', function (e) {
    //         var formData = new FormData();
    //         // formData.ppend(name, element);
    //         formData.append('file', $('input[id=excelFile]')[0].files[0]);
    //         $.ajax({
    //             url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category/importExcel",
    //             method: 'POST',
    //             data: formData,
    //             contentType: false, // 注意这里应设为false
    //             processData: false,
    //             cache: false,
    //             success: function (data) {
    //                 if (JSON.parse(data).result == 1) {
    //                     $('.prompt').html(`文件${JSON.parse(data).filename}已上传成功`);
    //                 }
    //             },
    //             error: function (jqXHR) {
    //                 console.log(JSON.stringify(jqXHR));
    //             }
    //         })
    //             .done(function (data) {
    //                 console.log('done');
    //             })
    //             .fail(function (data) {
    //                 console.log('fail');
    //             })
    //             .always(function (data) {
    //                 console.log('always');
    //             });
    //     });
    // });

    $("#jqGrid").jqGrid({
        url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + 'diy/category/list',
        datatype: "json",
        colModel: [
            {label: '主键', name: 'id', index: 'id', width: 50, key: true},
            {label: '名称（模块）', name: 'name', index: 'name', width: 80}
            , {label: '分类编码', name: 'code', index: 'code', width: 80}
            , {label: '排序', name: 'orderNum', index: 'order_num', width: 80}
            , {
                label: '类型', name: 'type', index: 'type', width: 80,
                formatter: function (value, options, row) {

                    let dictLabel = com.selectDictLabel(typeDatas, value);
                    console.log(dictLabel);

                    return dictLabel
                }
            } ,
            {label: '是否列表显示', name: 'isShow', index: 'is_show', width: 80,
                formatter: function (value, options, row) {
                    let dictLabel = com.selectDictLabel(yesNoDatas, value);
                    return dictLabel
                }
            } ,
            {label: '是否对外开放', name: 'isOpen', index: 'is_open', width: 80,
                formatter: function (value, options, row) {
                    let dictLabel = com.selectDictLabel(yesNoDatas, value);
                    return dictLabel
                }
            },
            {
                label: '图标',
                name: 'icon',
                index: 'icon',
                width: 80,
                align: 'center',classes:'diyCell',
                formatter: function (cellvalue, options, rowObject) {
                    if (cellvalue){
                    return '<img src="' + cellvalue + '" class="img-circle" style="width: 80px;height: 80px;"/>';
                    } else {
                        return "无"
                    }
                }
            }
            , {label: '备注信息', name: 'remark', index: 'remark', width: 80}
            ,],
        viewrecords: true,
        height: "100%",
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#myapp',
    data: {
        showList: true,
        title: null,
        params: {
            name: null
        },
        category: {},
        menu: {
            parentName: null,
            parentId: 0,
            type: 1,
            orderNum: 0
        },
        url: null,
        typeDatas:[]
    },
    created: function () {
        this.typeDatas = typeDatas
    },
    methods: {
        query: function () {
            //初始化查询分页为1，默认从第一页查
            $("#jqGrid").jqGrid('setGridParam',{
                //添加查询条件
                postData:{

                },
                page:1
            });
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.category = {
                type:3,
                isShow:1,
                isOpen:0,
            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.category.id == null ? "diy/category/save" : "diy/category/update";

            //请求参数
            var param = JSON.stringify(vm.category);

            com.AjaxPost((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + url, param, "json", function (r) {
                if (r.success) {
                    toast('操作成功');
                    vm.reload();
                } else {
                    warning(r.msg);
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category/delete";
                //请求参数
                var param = JSON.stringify(ids);
                com.AjaxPost(url, param, "json", function (r) {
                    if (r.success) {
                        toast('操作成功');
                        vm.reload();
                    } else {
                        warning(r.msg);
                    }
                });
            });
        },
        openImportExcel: function (event) {
            openImportExcel();
        },
        importExcel: function (event) {
            let prefix = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category";
            importExcel(prefix, null, vm);
        },
        exportExcel: function (event) {
            let prefix = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category";
            exportExcel(prefix);
        },
        exportExcelTemplate: function (event) {
            let prefix = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category";
            exportExcelTemplate(prefix);
        },
        openManageDiyCategoryAttributes: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openDialogView("字段管理", 'modules/diy/attribute.html?categoryId=' + id, '80%', '80%')
        },
        openAddCategoryApi: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openDialogView("查看API", 'webjars/static/qr/api_baseinfo.html?categoryId=' + id, '80%', '80%')
        },
        openAddCategoryMenu: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            $('#myModal').modal('show');
            vm.getInfo(id);
            //如果此操作复制失败，用dom方式
            // vm.menu.url = "modules/diy/baseinfo.html?categoryId=" + id;
            // $("#categoryMenuUrl").attr('value', "modules/diy/baseinfo.html?categoryId=" + id);
            // $("#categoryMenuName").attr('value', vm.category.name);
            // $("#categoryMenuOrderNum").attr('value', vm.category.orderNum);
            vm.menu.url = "modules/diy/baseinfo.html?categoryId=" + id;
            vm.menu.name = vm.category.name;
            vm.menu.orderNum = vm.category.orderNum;
            // top.layer.open({
            //     type: 1,
            //     area: ['80%', '80%'],
            //     title: "添加菜单",
            //     content: $("#categoryMenuBox").html(),
            //     btn: ['确定', '关闭'],
            //     btn1: function (index, layero) {
            //         var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category/addCategoryMenu";
            //         var inputForm = top.$("#categoryMenuForm");
            //         inputForm.attr('action', url);
            //         inputForm.submit();
            //         top.layer.close(index);
            //
            //         //请求参数
            //         // var param = JSON.stringify(id);
            //         // com.AjaxPost(url, param, "json", function (r) {
            //         //     if (r.success) {
            //         //         toast('操作成功');
            //         //         vm.reload();
            //         //     } else {
            //         //         warning(r.msg);
            //         //     }
            //         // });
            //
            //     },
            //     btn2: function (index) {
            //         top.layer.close(index);
            //     }
            // });
        }, saveCategoryMenu: function (event) {
            let url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category/addCategoryMenu";
            let param = JSON.stringify(vm.menu);
            com.AjaxPost( url, param, "json", function (r) {
                if (r.success) {
                    toast('操作成功');
                    $('#myModal').modal('hide');
                    vm.reload();
                } else {
                    warning(r.msg);
                }
            });
        },

        getInfo: function (id) {
            var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "diy/category/info/" + id;
            com.AjaxGet(url, null, "json", function (r) {
                vm.category = r.entity;
            }, null, false)
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                //添加查询条件
                postData: {
                    name: vm.params.name
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});
